package com.jacobson.core.widget.logger

/**
 * ================================================
 * 日志实现类
 * @author Wucz on 2018/7/17 10:55
 * ================================================
 */
class LoggerImpl internal constructor(
        private val tag: String? = "rootTag",
        private val pre: String? = null,
        private val printer: Printer = DefaultLoggerPrinter()
) : Logger {

    override fun v(msg: String, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.VERBOSE, transMsg(msg), *params)
    }

    override fun v(msg: String, tr: Throwable, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.VERBOSE, transMsg(msg), tr, *params)
    }

    override fun d(msg: String, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.DEBUG, transMsg(msg), *params)
    }

    override fun d(msg: String, tr: Throwable, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.DEBUG, transMsg(msg), tr, *params)
    }

    override fun i(msg: String, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.INFO, transMsg(msg), *params)
    }

    override fun i(msg: String, tr: Throwable, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.INFO, transMsg(msg), tr, *params)
    }

    override fun w(msg: String, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.WARN, transMsg(msg), *params)
    }

    override fun w(msg: String, tr: Throwable, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.WARN, transMsg(msg), tr, *params)
    }

    override fun e(msg: String, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.ERROR, transMsg(msg), *params)
    }

    override fun e(msg: String, tr: Throwable, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.ERROR, transMsg(msg), tr, *params)
    }

    override fun wtf(msg: String, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.ASSERT, transMsg(msg), *params)
    }

    override fun wtf(msg: String, tr: Throwable, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.NORMAL, Logger.Level.ASSERT, transMsg(msg), tr, *params)
    }

    override fun json(json: String, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.JSON, Logger.Level.DEBUG, json, *params)
    }

    override fun json(@LogLevel level: Int, json: String, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.JSON, level, json, *params)
    }

    override fun xml(xml: String, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.XML, Logger.Level.DEBUG, xml, *params)
    }

    override fun xml(@LogLevel level: Int, xml: String, vararg params: Any): Int {
        return this.printer.doPrint(chooseTag(), Logger.Type.XML, level, xml, *params)
    }

    private fun chooseTag(): String {
        return "rootTag"
    }

    private fun transMsg(msg: String): String {
        return (pre?:"") + msg
    }
}